package com.scd.jmeter.rmi.server;

import com.scd.jmeter.rmi.common.RmiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class RemoteMain {
    private static final Logger log = LoggerFactory.getLogger(RemoteMain.class);

    public static int DEFAULT_RMI_REGISTRY_PORT;

    public static int DEFAULT_RMI_PORT;

    public static int DEFAULT_LOCAL_PORT;

    public static void main(String[] args) throws IOException {
        String filePath = "jmeter.properties";
        if (args.length > 0) {
            filePath = args[0];
        }
        log.info("加载配置文件路径 {}", filePath);
        Properties properties = new Properties();
        properties.load(new FileInputStream(filePath));
        log.info("开始启动 RMI 远程服务");
        RemoteJMeterEngineImpl.startServer(getRmiRegistryPort(properties));
        log.info("RMI 远程服务启动成功");
    }

    public static int getRmiRegistryPort(Properties properties) {
        DEFAULT_RMI_REGISTRY_PORT = (int) properties.getOrDefault("server_port", 0);
        DEFAULT_RMI_PORT = (int) properties.getOrDefault("server.rmi.port", 1099);
        DEFAULT_RMI_REGISTRY_PORT = (int) properties.getOrDefault("server.rmi.localport", 0);
        return DEFAULT_RMI_REGISTRY_PORT == 0 ?
                DEFAULT_RMI_PORT : DEFAULT_RMI_REGISTRY_PORT;
    }
}
